'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 更换对手
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var battleModel = require('../../../../models/battleModel');
var rivalService = require('../../../../services/rivalService');
var roleService = require('../../../../services/roleService');
var heroConf = require('../../../../config/heroConf');
var PlayerManager = require('../../../../libs/playerManager');
var constantUtil = require('../../../../util/constantUtil');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var road_id = msg.road_id;
    if (!road_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var rival_loader;
    var road_player;
    var rival_info;
    var pvp_model;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'pvp', 'task', 'achieve', 'daily_task', 'battle', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var battle_model = player_manager.battle;
                pvp_model = player_manager.pvp;

                road_player = pvp_model.getRoadPlayer(road_id);
                if (!road_player) {
                    return next(null, {
                        code: Code.BATTLE_RIVAL_NOT_EXIST
                    });
                }

                if (!pvp_model.decFreeChangeRivalCnt()) {
                    var pvp_fresh_point_cost = constantUtil.value('pvp_fresh_point_cost');
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_PVP, operationType.OPR_PVP_CHANGE_RIVAL, pvp_fresh_point_cost)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }
                }

                rivalService.getRandomRivalRoleId(role_id, pvp_model.getPlayerRoleIdList(), cb);
            },
            function (rival_role_id, cb) {
                road_player.role_id = rival_role_id;
                rival_loader = new PlayerManager(rival_role_id);
                rival_loader.getModel(['role'], cb);
            },
            function (cb) {
                rivalService.getInfo(rival_loader, cb);
            },
            function (info, cb) {
                rival_info = info;
                rival_info.road_id = road_id;
                pvp_model.rival_gold = rival_info.gold;
                pvp_model.rival_food = rival_info.food;
                cb();
            },
            function (cb) {
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    rival: rival_info
                }
            });
        }
    );
};
